//
//  WBNewFeatureView.swift
//  DLSwift微博
//
//  Created by 丁浪涛 on 2016/11/17.
//  Copyright © 2016年 丁浪涛. All rights reserved.
//

import UIKit
//新特性视图
class WBNewFeatureView: UIView {
    @IBOutlet weak var pageControl: UIPageControl!

    @IBOutlet weak var EnterBtn: UIButton!
    @IBOutlet weak var newScrollerView: UIScrollView!
    
    @IBAction func enterStatus(_ sender: Any) {
        
        removeFromSuperview()
    }

    class func  newfeatureView() ->WBNewFeatureView
    {
    
    let  nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
    let arr = nib.instantiate(withOwner: nil, options: nil)
    let v = arr[0] as! WBNewFeatureView
    v.frame = UIScreen.main.bounds
        
     return v
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //如果使用自动布局设置的界面  从XIB加载的是默认的600*600大小
        
        let count = 4
        let rect = UIScreen.main.bounds
        for i in 0..<count
            {
        let  imageName = "new_feature_\(i+1)"
        let iv = UIImageView(image: UIImage(named: imageName))
                iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
                newScrollerView.addSubview(iv)
        }
        
        newScrollerView.contentSize = CGSize(width: CGFloat(count+1)*rect.width, height: rect.height)
        newScrollerView.bounces = false
        newScrollerView.isPagingEnabled = true
        newScrollerView.showsVerticalScrollIndicator = false
        newScrollerView.showsHorizontalScrollIndicator = false
        
        EnterBtn.isHidden =  true
        
        newScrollerView.delegate = self
        
    }
    
}

extension WBNewFeatureView: UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //滚动到最后一屏 让视图删除
        let page     = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        print(page)
   
        if page == scrollView.subviews.count {
          print("欢迎欢迎")
            removeFromSuperview()
        }
        
        //如果是倒数第二页 显示按钮
         EnterBtn.isHidden = (page != scrollView.subviews.count - 1)
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        EnterBtn.isHidden  = true
        let page   = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
         pageControl.currentPage = page
        
        //分页空间的隐藏
        pageControl.isHidden = (page == scrollView.subviews.count)
    }

}
